﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#pragma once

#include "../PSRDOP2.h"
#include "LogHeaderDecoder.h"

namespace GnssLogs
{
namespace PSRDOP2Decoder
{
using namespace tao::pegtl;
using Sync   = TAO_PEGTL_STRING("#PSRDOP2A");
using Header = LogHeaderDecoder::SyncExpression<Sync>;
// clang-format off
struct GDOP : Floating {};
struct PDOP : Floating {};
struct HDOP : Floating {};
struct VDOP : Floating {};
struct SystemCount : plus<digit> {};
struct SystemUsedForTimingRule : ascii::identifier {};
struct TDOP : Floating {};
using System = seq<Split, SystemUsedForTimingRule, Split, TDOP>;
// clang-format on

using Expression = seq<Header, GDOP, Split, PDOP, Split, HDOP, Split, VDOP, Split, SystemCount, star<System>, one<'*'>, CRC32>;

template<typename T>
struct Action
{
};

template<>
struct Action<Header> : LogHeaderDecoder::LogHeaderHelperNoSetValue
{
};

template<>
struct Action<GDOP> : FloatHelper<Action<GDOP>>
{
    static void setValue(PSRDOP2::Data& data, float value) { data.gdop = value; }
};

template<>
struct Action<PDOP> : FloatHelper<Action<PDOP>>
{
    static void setValue(PSRDOP2::Data& data, float value) { data.pdop = value; }
};

template<>
struct Action<HDOP> : FloatHelper<Action<HDOP>>
{
    static void setValue(PSRDOP2::Data& data, float value) { data.hdop = value; }
};

template<>
struct Action<VDOP> : FloatHelper<Action<VDOP>>
{
    static void setValue(PSRDOP2::Data& data, float value) { data.vdop = value; }
};

template<>
struct Action<SystemCount> : IntHelper<Action<SystemCount>, uint32_t>
{
    static void setValue(PSRDOP2::Data& data, uint32_t value) { data.systemCount = value; }
};

template<>
struct Action<SystemUsedForTimingRule> : EnumHelper<Action<SystemUsedForTimingRule>, SystemUserdForTiming>
{
    static void setValue(PSRDOP2::Data& data, SystemUserdForTiming value)
    {
        data.systems.push_back({});
        data.systems.back().system = value;
    }
};

template<>
struct Action<TDOP> : FloatHelper<Action<TDOP>>
{
    static void setValue(PSRDOP2::Data& data, float value) { data.systems.back().tdop = value; }
};

template<>
struct Action<CRC32> : CRC32Helper<Action<CRC32>>
{
    static void setValue(PSRDOP2::Data& data, uint32_t value) { data.crc32 = value; }
};
} // namespace PSRDOP2Decoder
} // namespace GnssLogs